﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MiniFox.Security;

namespace MiniFox.Security.Text
{
    /// <summary>
    /// 验证码对象, 此对象可通过string形式的对象隐式转换得到，例如： ValidCode validCode = "abcd";
    /// 同样，验证码对象可隐式转换为string对象, 注意：转换为字符串时,验证码对象将被转为md5加密字符串
    /// </summary>
    public class ValidCode
    {
        string _originalCode;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="validCode"></param>
        protected internal ValidCode(string validCode)
        {
            _originalCode = validCode;
        }
        /// <summary>
        /// 原始的验证码明文字符串
        /// </summary>
        protected internal string OriginalValue
        {
            get { return _originalCode; }
        }
        /// <summary>
        /// 加密后的验证码字符串，默认使用MD5加密，可重写此方法
        /// </summary>
        public virtual string FinalValue
        {
            get { return _originalCode.MD5EncryptToHexString(); }
        }
        /// <summary>
        /// 转换为加密后的验证码字符串
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return this.FinalValue;
        }
        /// <summary>
        /// 普通字符串转为验证码
        /// </summary>
        /// <param name="validCodeString"></param>
        /// <returns></returns>
        public static implicit operator ValidCode(string validCodeString)
        {
            return new ValidCode(validCodeString);
        }
        /// <summary>
        /// 验证码对象转为md5加密字符串
        /// </summary>
        /// <param name="validCode"></param>
        /// <returns></returns>
        public static implicit operator string(ValidCode validCode)
        {
            return validCode.ToString();
        }
    }
}
